﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Business;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Crypto;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
   // [Authorize]
    [ApiController]
    public class UserRecipeController : ControllerBase
    {
        private IUserRecipeService _userRecipeService;

        public UserRecipeController(IUserRecipeService userRecipeService)
        {
            _userRecipeService = userRecipeService;
        }
        [HttpGet]
        public ApiResponseData GetAllUserRecipeData()
        {
            List<UserRecipe> model = _userRecipeService.GetAllUserRecipes();
            return ResultHelper.Success(new ReturnData() { total = model.Count, dataList = model });
        }
        [HttpPost]
        public ApiResponseData GetUserRecipeData(UserRecipeInput input)
        {
            List<UserRecipe> modelList = _userRecipeService.GetAllUserRecipes();
            if (!input.RecipeId.IsNullOrEmpty())
            {
                modelList = modelList.Where(i => i.RecipeId.Contains(input.RecipeId)).ToList();
            }
            if (!input.ParameterId.IsNullOrEmpty())
            {
                modelList = modelList.Where(i => i.ParameterId.Contains(input.ParameterId)).ToList();
            }
            int count = modelList.Count;
            if (!modelList.IsNullOrEmpty())
            {
                int start = (input.page - 1) * input.size;
                int end = ((start + input.size) >= (modelList.Count)) ? (modelList.Count) : (start + input.size);
                modelList = modelList.GetRange(start, end - start);
            }
            //List<UserRecipeModel> models = _userRecipeService.MendingRange(modelList);
            return ResultHelper.Success(new ReturnData() { total = count, dataList = modelList });
        }
        [HttpPost]
        public ApiResponseData AddUserRecipe(UserRecipe model)
        {
            //重复性判断
            if (_userRecipeService.CheckRepeated(model))
            {
                return ResultHelper.Error(502, "ID cannot be repeated! ");
            }
            if (_userRecipeService.addUserRecipe(model))
            {
                return ResultHelper.Success("Add successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to add!");
            }
        }
        [HttpPost]
        public ApiResponseData UpdateUserRecipe(UserRecipe model)
        {
            if (_userRecipeService.GetUserRecipeByIds(model.RecipeId,model.ParameterId) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + model.RecipeId + "!");
            }
            if (_userRecipeService.updateUserRecipe(model))
            {
                return ResultHelper.Success("Update successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Update!");
            }
        }
        [HttpPost]
        public ApiResponseData DeleteUserRecipe(DeleteStringTwoId ids)
        {
            if (_userRecipeService.GetUserRecipeByIds(ids.recipeId, ids.parameterId) == null)
            {
                return ResultHelper.Error(502, "The data does not exist !");
            }
            if (_userRecipeService.DeleteUserRecipe(ids.recipeId, ids.parameterId))
            {
                return ResultHelper.Success("Delete successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Delete!");
            }
        }
        [HttpPost]
        public ApiResponseData BatchDelUserRecipe(List<UserRecipe> batchRemoveDatas)
        {
            if (batchRemoveDatas.Count > 0)
            {
                if (_userRecipeService.BatchDelUserRecipe(batchRemoveDatas))
                {
                    return ResultHelper.Success("Delete successfully!");
                }
                else
                {
                    return ResultHelper.Error(500, "Failed to Delete!");
                }
            }
            return ResultHelper.Error(502, "No data detected!");
        }

    }
}
